package com.echo.echovividlive.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.echo.echovividlive.common.UserThread;
import com.echo.echovividlive.domain.dto.FindPasswordDTO;
import com.echo.echovividlive.domain.dto.PageQueryDTO;
import com.echo.echovividlive.domain.dto.PasswordResetVO;
import com.echo.echovividlive.domain.dto.UserRegisterDTO;
import com.echo.echovividlive.domain.entity.Result;
import com.echo.echovividlive.domain.entity.User;
import com.echo.echovividlive.service.IUserInteractService;
import com.echo.echovividlive.service.IUserService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiajunjie
 * @since 2024-10-22
 */
@Slf4j
@RestController
@RequestMapping("/echo/user")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;

    private final IUserInteractService userInteractService;

    /**
     * 发送验证码
     * @param user
     * @return
     */
    @PostMapping("/requestVerifyCode")
    public Result requestVerifyCode(@RequestBody UserRegisterDTO user) {
        if(user==null){
            return Result.error("输入为空");
        }
        User one = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getEmail, user.getEmail()));
        if(one != null){
            return Result.error("该邮箱已被注册");
        }
        log.info("用户注册验证码请求参数:{}",user);
        // return Result.success("验证码发送成功!","success");
        return userService.sendVerifyCode(user);
    }

    /**
     * 激活账户
     * @param user
     * @return
     */
    @PostMapping("/active")
    public Result active(@RequestBody UserRegisterDTO user) {
        if(user==null){
            return Result.error("输入为空");
        }
        log.info("激活用户参数:{}",user);
        return userService.active(user);
    }

    /**
     * 用户登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        if(user==null){
            return Result.error("输入为空");
        }
        log.info("用户登录参数:{}",user);
        return userService.login(user);
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Result getUserById(@PathVariable("id") Integer id) {
        log.info("根据id查询用户参数:{}",id);
        User user = userService.getById(id);
        if(user==null){
            return Result.error("用户不存在");
        }
        user.setPassword(null);
        user.setBalance(null);
        return Result.success(user);
    }

    /**
     * 查询自己
     * @return
     */
    @GetMapping("/getCurrentUserInfo")
    public Result getCurrentUserInfo() {
        int userId = UserThread.getUserId();
        User user = userService.getById(userId);
        user.setPassword(null);
        return Result.success(user);
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        if(user==null){
            return Result.error("数据为空");
        }
        if (!user.getUserId().equals(UserThread.getUserId())) {
            return Result.error("非法操作");
        }
        log.info("更新用户参数:{}",user);
        User one = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getEmail, user.getEmail()));
        if(!one.getUserId().equals(user.getUserId())){
            return Result.error("该邮箱已被注册");
        }
        if (userService.updateById(user)) {
            return Result.success(user);
        }
        return Result.error("更新失败");
    }

    /**
     * 上传头像
     * @param avatar
     *
     * @return
     */
    @PostMapping("/avatar")
    public Result uploadAvatar(@RequestParam("avatar") MultipartFile avatar) {
        return userService.uploadAvatar(avatar);
    }

    /**
     * 上传头像
     * @param request
     *
     * @return
     */
    @GetMapping("/avatar/stream")
    public Result uploadAvatarByStream(HttpServletRequest request) throws IOException {

        return userService.uploadAvatarByStream(request);
    }
    /**
     * 关注
     * @param userId
     * @return
     */
    @GetMapping("/following/{userId}")
    public Result following(@PathVariable Integer userId) {
        return userInteractService.following(userId);
    }

    /**
     * 判断是否关注过
     * @param userId
     * @return
     */
    @GetMapping("/following/check/{userId}")
    public Result checkFollowing(@PathVariable Integer userId) {
        return userInteractService.checkFollowing(userId);
    }

    /**
     * 获取关注列表
     * @return
     */
    @PostMapping("/getFollowingInfo")
    public Result getFollowingInfo(@RequestBody PageQueryDTO pageQueryDTO) {
        return userInteractService.getFollowingList(pageQueryDTO);
    }

    /**
     *  修改密码
     */
    @PostMapping("/password/reset")
    public Result resetPassword(@RequestBody PasswordResetVO passwordResetVO) {
        log.info("修改密码参数:{}",passwordResetVO);
        return userService.resetPassword(passwordResetVO);
    }

    @PostMapping("/findPassword")
    public Result findPassword(@RequestBody FindPasswordDTO findPasswordDTO) {
        return userService.findPassword(findPasswordDTO);
    }

    @PostMapping("/getFans")
    public Result getFans(@RequestBody PageQueryDTO pageQueryDTO) {
        return userInteractService.getFans(pageQueryDTO);
    }
}
